package com.glorysoft.sunnypda.ui.updateIp

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivityUpdateEqpIpBinding
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_update_eqp_ip.*

/**
 * FileName: UpdateEqpIpActivity
 * Author: XJH
 * Date: 2022/1/11 16:07
 * Description:
 * History:
 */
class UpdateEqpIpActivity : BaseActivity<UpdateIpViewModel, ActivityUpdateEqpIpBinding>(),
    View.OnClickListener {
    var ip: String = ""
    private var linePop: PopWindowsUtils.GeneralPop? = null
    private var posPop: PopWindowsUtils.GeneralPop? = null
    private var eqpTypePop: PopWindowsUtils.GeneralPop? = null

    override fun layoutId(): Int {
        return R.layout.activity_update_eqp_ip
    }

    override fun initVariableId(): Int {
        return BR.updateIpViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.change_equipment_ip))

        initEdit(etEqpWork, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                tvEqpType.text = ""
                tvStep.text = ""
                tvLine.text = ""
                tvPos.text = ""
                etIP.setText("")
                etPort.setText("")
                viewModel.getEqpInfo(s)
            }

        })


        flLineList.setOnClickListener(this)
        flPosList.setOnClickListener(this)
        flEqpTypeList.setOnClickListener(this)

        setRightListener(getString(R.string.confirm)) {
//            if (ip == etIP.text.toString().trim()) {
//                viewModel.defUI.toastEvent.value =
//                    ToastMessageBean(getString(R.string.IP_no_change), false)
//                return@setRightListener
//            }

            if (etEqpWork.text.toString().isEmpty()) {
                viewModel.defUI.toastEvent.value =
                    ToastMessageBean(getString(R.string.eqp_hint), false)
                return@setRightListener
            }
            if (etIP.text.toString().isEmpty()) {
                viewModel.defUI.toastEvent.value =
                    ToastMessageBean(getString(R.string.input_server_address), false)
                return@setRightListener
            }
            if (etPort.text.toString().isEmpty()
            ) {
                viewModel.defUI.toastEvent.value =
                    ToastMessageBean(getString(R.string.port_no), false)
                return@setRightListener
            }

            viewModel.updateIP(
                etEqpWork.text.toString().trim(),
                etIP.text.toString().trim(),
                tvEqpType.text.toString(),
                etPort.text.toString(),
                tvLine.text.toString(),
                tvPos.text.toString()
            )

        }
    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                0 -> {
                    val entity = it.item as EqpInfoEntity
                    tvEqpType.text = if (entity.EQPTYPE == null) "" else entity.EQPTYPE
                    tvStep.text = if (entity.FAB == null) "" else entity.FAB
                    ip = if (entity.EQPIP == null) "" else entity.EQPIP!!
                    etIP.setText(ip)
                    tvLine.text = if (entity.LINEID == null)"" else entity.LINEID+":"+if (entity.LINE == null)"" else entity.LINE
                    tvPos.text = if (entity.POSITIONID == null)"" else entity.POSITIONID+":"+entity.POSITION
                    etPort.setText(if (entity.EQPPORT == null) "" else entity.EQPPORT)
//                    if (entity.EQPTYPE == null || entity.EQPTYPE == "") {
//                        tvEqpType.visibility = View.GONE
//                        flEqpTypeList.visibility = View.VISIBLE
//                    } else {
//                        tvEqpType.visibility = View.VISIBLE
//                        flEqpTypeList.visibility = View.GONE
//                    }
                    viewModel.getFabList()
                }
            }

        }

        viewModel.defUI.startActivity.observe(this) {
            viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.success), true)
            finish()
        }

    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.flLineList -> {
                if (viewModel.lineList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }

                linePop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flLineList.width,
                    viewModel.lineList,
                    ivLineArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null") {
                                tvLine.text = ""
                            } else {
                                tvLine.text = codeS
                            }
                            tvPos.text = ""
                            if (codeS != "") {
                                viewModel.getPositionList(codeS.split(":")[1])
                            }
                        }

                    })



                if (linePop!!.isShowing) {
                    linePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivLineArrow)
                    linePop?.showAsDropDown(flLineList, 0, 0)
                }
            }
            R.id.flPosList -> {
                if (viewModel.posList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }

                posPop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flPosList.width,
                    viewModel.posList,
                    ivPos,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null") {
                                tvPos.text = ""
                            } else {
                                tvPos.text = codeS
                            }
                        }

                    })



                if (posPop!!.isShowing) {
                    posPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivPos)
                    posPop?.showAsDropDown(flPosList, 0, 0)
                }
            }
            R.id.flEqpTypeList -> {
                if (viewModel.eqpTypeList.isEmpty()) {
                    viewModel.defUI.toastEvent.value =
                        ToastMessageBean(getString(R.string.return_area_is_empty), false)
                    return
                }

                eqpTypePop = PopWindowsUtils.getOneChoosePop(
                    this,
                    flEqpTypeList.width,
                    viewModel.eqpTypeList,
                    ivEqpTypeArrow,
                    object :
                        PopWindowsUtils.PopTextAndPositionCallback {
                        @RequiresApi(Build.VERSION_CODES.P)
                        override fun onSuccess(codeS: String, potion: Int) {
                            if (codeS == "null") {
                                tvEqpListType.text = ""
                            } else {
                                tvEqpListType.text = codeS
                            }
                        }

                    })



                if (eqpTypePop!!.isShowing) {
                    eqpTypePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivEqpTypeArrow)
                    eqpTypePop?.showAsDropDown(flEqpTypeList, 0, 0)
                }
            }
        }
    }
}